var User = (function(){
	var USER_REG = {};
	var login_CallBack = function(data){
		Build.getCookie("JSESSIONID");
		if(data.code == "200"){
			var data = data.data;
			Build.saveCache("USER",data);
			$(".modal-wall,.modal-wrap").hide();
			$.toast("登陆成功");
		}else if(data.code == "402"){
			$.toast("密码错误","forbidden",function(){
				$("#login_pwd").val("");
			});
		}else{
			$.toast("不存在该用户","forbidden");
			$("#login_pwd").val("");
		}
	};
	var getCode_CallBack = function(data){
		USER_REG.code = data.data.code;
		USER_REG.id = data.data.id;
		//显示输入验证码
		$("#reg-wrap").hide();
		$("#code-wrap").show();
		$.toast("验证码为" + data.data.code);
	};
	var reg_CallBack = function(data){
			if(data.code == "410"){
				$.toast("注册成功,请登录");
				$("#info-wrap").hide();
				$("#login-wrap").show();
			}else if(data.code == "412"){
				$.toast("该用户已注册过本产品,请直接登陆","forbidden");
				$("#info-wrap").hide();
				$("#login-wrap").show();
			}else{
				$.toast("注册失败,请重新注册");
				$("#info-wrap").hide();
				$("#reg-wrap").show();
			}
	};
	return {
		login: function(){
			var phone = $("#login_phone").val();
			var pwd = $("#login_pwd").val();
			if(!(/^1[34578]\d{9}/.test(phone))){
				$.toast("手机号格式错误","forbidden",function(){
					$("#login_pwd").val("");
				});
			}else if(!(/^[\w\W]{6,}$/.test(pwd))){
				$.toast("输入密码不少于6位","forbidden",function(){
					$("#login_pwd").val("");
				});
			}else{
				$.showLoading("正在登陆");
				Build.request({
					type: "get",  
					data: {
						phoneNumber: phone,
						passWord: pwd,
					}, 
					port: "/user/login"
				}, login_CallBack, false);
			}
		},
		getCode: function() {
			var phone = $("#reg_phone").val();
			if(!(/^1[34578]\d{9}/.test(phone))) {
				$.toast("手机号码不正确","forbidden",function(){
					$("#reg_phone").val("");
				});
			}else{
				USER_REG.phoneNumber = phone;
				Build.saveCache("USER_REG", "phoneNumber", phone); 
				$.showLoading("正在发送验证码");
				Build.request({
					type: "get", 
					data: {
						phoneNumber: phone
					}, 
					port: "/user/getVerificationCode"
				}, getCode_CallBack, false);
			} 
		},
		checkInCode: function(){
			var incode = "";
			$(".tel-code").each(function(i){
				incode += $(this).val();
			})
			if(incode != USER_REG.code){
				$.toast("验证码错误,请重新输入","forbidden");	
				$(".tel-code").each(function(i){
					$(this).val("");
				})
			}else{
				$("#code-wrap").hide();
				$("#pwd-wrap").show();
			}
		},
		inPassWord: function(){
			var pwd = $("#reg_pwd").val();
			if(!(/^[\w\W]{6,}$/.test(pwd))){
				$.toast("密码不能小于6位","forbidden",function(){
					$("#reg_pwd").val("");
				});
			}else{
				USER_REG.passWord = pwd;
				$("#pwd-wrap").hide();
				$("#info-wrap").show();
			}
		},
		reg: function(){
			var firstName = $("#reg_lastName").val();
			var lastName = $("#reg_firstName").val();
			var sex = $("#reg_sex").val();
			if(!/^[a-zA-Z\u4e00-\u9fa5]+$/.test(firstName) && 
				/^[a-zA-Z\u4e00-\u9fa5]+$/.test(
					lastName) &&
				/^[a-zA-Z\u4e00-\u9fa5]+$/.test(sex)){
				$.toast("只能输入中文和字母","forbidden",function(){
					$("#reg_firstName,#reg_lastName,#reg_sex").val("");
				})
			}else{
				USER_REG.firstName = firstName;
				USER_REG.lastName = lastName;
				USER_REG.sex = sex;

				$.showLoading("正在登陆");
				Build.request({
					type: "get",
					data: USER_REG,
					port: "/user/signUp"
				},reg_CallBack, false);
			}
		}
	}
})(jQuery)